package com.yilian.xlmusic.android.utils;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

    /**
     * 关键字变色
     * @param color
     * @param text
     * @param keyword
     * @return
     */
    public static SpannableString matcherSearchTitle(int color, String text, String keyword) {
        String string = text.toLowerCase();
        String key = keyword.toLowerCase();
        Pattern pattern = Pattern.compile(key);
        Matcher matcher = pattern.matcher(string);
        SpannableString ss = new SpannableString(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(color), start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    //加载圆角图片
    public static void loadRoundImage(String url, final ImageView imageView,int corner) {
        //图片加载自己实现
        Glide.with(imageView)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(corner)))
                .into(imageView);
    }

    //加载圆角图片
    public static void loadRoundImage(int url, final ImageView imageView,int corner) {
        Glide.with(imageView)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(corner)))
                .into(imageView);
    }
    public static void loadRoundImage(int url, final ImageView imageView) {
        Glide.with(imageView)
                .load(url)
                .into(imageView);
    }
    public static void loadRoundImage(String url, final ImageView imageView) {
        Glide.with(imageView)
                .load(url)
                .into(imageView);
    }
}
